package org.blueoxygen.kukuruyuk.actions.plasma;
import java.sql.SQLException;
import java.util.List;
import org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactory;
import org.blueoxygen.cimande.persistence.hibernate.HibernateSessionFactoryAware;
import org.blueoxygen.kukuruyuk.entity.Plasma;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
public class SearchPlasma extends PlasmaForm
		implements HibernateSessionFactoryAware {
		private String dropValue;
		private HibernateSessionFactory hsf;
		private Session sess;
		private int maxPage, currPage, nextPage, prevPage = 0, page = 0;
		private int maxRowPerPage = 10;
		private String orderBy = "name";
		private int resultRows;
		public String execute(){
				sess = hsf.createSession();
				Criteria crit = sess.createCriteria(Plasma.class);
				if(getDropValue() != null
						&& !"".equalsIgnoreCase(getDropValue())){
					crit.add(Expression.like("region", getDropValue(),
							MatchMode.ANYWHERE));
				}
				
				if(getPlasma().getName() != null
						&& !"".equalsIgnoreCase(getPlasma().getName())){
					crit.add(Expression.like("name", getPlasma().getName(),
							MatchMode.ANYWHERE));
				}
				
				resultRows = crit.list().size();
				maxPage = resultRows / maxRowPerPage;
				prevPage = currPage - 1;
				nextPage = currPage + 1;
				page = currPage + 1;
			if (resultRows % maxRowPerPage == 0) maxPage = maxPage -1;
			setPlasmas(crit.addOrder(Order.asc(orderBy))
					.setFirstResult(currPage * maxRowPerPage)
					.setMaxResults(maxRowPerPage)
					.list());
			try {
				hsf.endSession((sess));
				hsf.closeSession(sess);
			} catch (HibernateException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
		}
		return SUCCESS;
		}
		
		public String getDropValue() {
			return dropValue;
		}

		public void setDropValue(String dropValue) {
			this.dropValue = dropValue;
		}
		
		public void setHsf(HibernateSessionFactory hsf) {
			this.hsf = hsf;
		}

		public Session getSess() {
			return sess;
		}

		public void setSess(Session sess) {
			this.sess = sess;
		}

		public int getMaxPage() {
			return maxPage;
		}
		public void setMaxPage(int maxPage) {
		this.maxPage = maxPage;
		}
		public int getCurrPage() {
		return currPage;
		}
		public void setCurrPage(int currPage) {
		this.currPage = currPage;
		}
		public int getNextPage() {
		return nextPage;
		}
		public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
		}
		public int getPrevPage() {
		return prevPage;
		}
		public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
		}
		public int getMaxRowPerPage() {
		return maxRowPerPage;
		}
		public void setMaxRowPerPage(int maxRowPerPage) {
		this.maxRowPerPage = maxRowPerPage;
		}
		public String getOrderBy() {
		return orderBy;
		}
		public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
		}
		public int getResultRows() {
		return resultRows;
		}
		public void setResultRows(int resultRows) {
		this.resultRows = resultRows;
		}
		public int getPage() {
		return page;
		}
		public void setPage(int page) {
		this.page = page;
		}

		@Override
		public void setHibernateSessionFactory(HibernateSessionFactory arg0) {
			// TODO Auto-generated method stub
			this.hsf = arg0;
		}
}
